package com.zhsj.OkeHomeConsumption.ui.activity.citypartner;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyLazyFragment;
import com.zhsj.OkeHomeConsumption.common.OkeHomeConsumptionApi;
import com.zhsj.OkeHomeConsumption.empty.CityLookpayeeBean;
import com.zhsj.OkeHomeConsumption.empty.CitypantnerRemake;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.empty.Remake;
import com.zhsj.OkeHomeConsumption.event.LoginUserpanterEvent;
import com.zhsj.OkeHomeConsumption.other.AbastractDragFloatActionButton;
import com.zhsj.OkeHomeConsumption.ui.activity.HomeActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.LoginActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.WebActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.CityUIInterFace;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.CitypayeePresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.wallet.CityWalletActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.wallet.WalletActivity;
import com.zhsj.OkeHomeConsumption.utils.JsonUtils;
import com.zhsj.OkeHomeConsumption.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 城市合伙人首页
 */

public class CityPartnerActivity extends MyLazyFragment<HomeActivity> implements View.OnClickListener, CityUIInterFace {

    private TextView citypartnerpeole_payee;
    private TextView citypartnerpeole_lookpayee;
    private TextView citypartnerpeole_icome;
    private TextView citypartnerpeole_alreadly_despoit;
    private TextView citypartnerpeole_balance;
    private TextView citypartnerpeole_despoit;
    private LinearLayout citypartnerpeole_merchants_recruit;
    private LinearLayout citypartnerpeole_company_recruit;
    private LinearLayout citypartnerpeole_merchants_maintain;
    private LinearLayout citypartnerpeole_system;
    private LinearLayout citypartnerpeole_merchants_invate;
    private LinearLayout citypartnerpeole_company_invate;
    private LinearLayout zongshouru_lin;

    private LinearLayout yue_lin;

    private LinearLayout yitixian_lin;
    private String extractCash;
    private String LaveMoney;
    private String todayIncome;

    private TextView textView16;
    private AbastractDragFloatActionButton button = null;
    private CitypantnerRemake remake;

    private FormalUserInfo userinfo;
    private List<CityLookpayeeBean> extractCashRecordarr_list1;
    private List<CityLookpayeeBean> extractCashRecordarr_list2;
    private List<CityLookpayeeBean> extractCashRecordarr_list3;

    private CitypayeePresenter  minePersonPresenter;

    public static CityPartnerActivity newInstance() {
        return new CityPartnerActivity();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_city_partner;
    }

    @Override
    protected int getTitleId() {
        return R.id.citypartner_title;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void initView() {
        userinfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        minePersonPresenter = new CitypayeePresenter(getActivity(), this);
        citypartnerpeole_payee = (TextView) findViewById(R.id.citypartnerpeole_payee);
        citypartnerpeole_lookpayee = (TextView) findViewById(R.id.citypartnerpeole_lookpayee);
        citypartnerpeole_lookpayee.setOnClickListener(this);
        citypartnerpeole_icome = (TextView) findViewById(R.id.citypartnerpeole_icome);
        citypartnerpeole_alreadly_despoit = (TextView) findViewById(R.id.citypartnerpeole_alreadly_despoit);
        citypartnerpeole_balance = (TextView) findViewById(R.id.citypartnerpeole_balance);
        citypartnerpeole_despoit = (TextView) findViewById(R.id.citypartnerpeole_despoit);
        citypartnerpeole_merchants_recruit = (LinearLayout) findViewById(R.id.citypartnerpeole_merchants_recruit);
        citypartnerpeole_merchants_recruit.setOnClickListener(this);
        citypartnerpeole_company_recruit = (LinearLayout) findViewById(R.id.citypartnerpeole_company_recruit);
        citypartnerpeole_merchants_maintain = (LinearLayout) findViewById(R.id.citypartnerpeole_merchants_maintain);
        citypartnerpeole_merchants_maintain.setOnClickListener(this);
        citypartnerpeole_system = (LinearLayout) findViewById(R.id.citypartnerpeole_system);
        citypartnerpeole_merchants_invate = (LinearLayout) findViewById(R.id.citypartnerpeole_merchants_invate);
        citypartnerpeole_company_invate = (LinearLayout) findViewById(R.id.citypartnerpeole_company_invate);
        citypartnerpeole_company_recruit = (LinearLayout) findViewById(R.id.citypartnerpeole_company_recruit);
        yue_lin = findViewById(R.id.yue_lin);
        yue_lin.setOnClickListener(this);
        zongshouru_lin = findViewById(R.id.zongshouru_lin);
        yitixian_lin = findViewById(R.id.yitixian_lin);
        yitixian_lin.setOnClickListener(this);
        zongshouru_lin.setOnClickListener(this);
        button = findViewById(R.id.button);
        button.setOnClickListener(new AbastractDragFloatActionButton.OnClickListener() {
            @Override
            public void onClick() {
                EventBus.getDefault().post(new LoginUserpanterEvent(0));
            }
        });
        citypartnerpeole_company_recruit.setOnClickListener(this);
        citypartnerpeole_system.setOnClickListener(this);
        citypartnerpeole_merchants_invate.setOnClickListener(this);
        citypartnerpeole_company_invate.setOnClickListener(this);
        citypartnerpeole_despoit.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        minePersonPresenter.WalletGet();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.citypartnerpeole_lookpayee:
                //查询收入记录
                Intent intent = new Intent(getActivity(), LookpayeeActivity.class);
                intent.putExtra("flage",0);
                intent.putExtra("extractCashRecordarr_list", (Serializable) extractCashRecordarr_list1);
                intent.putExtra("extractCash",todayIncome);
                startActivity(intent);
                break;
            case R.id.citypartnerpeole_merchants_recruit:
                //商家招募
                startActivity(MerchantsRecruitActivity.class);
                break;
            case R.id.citypartnerpeole_merchants_maintain:
                //商家维护
                startActivity(MerchantsWeihuRecruitActivity.class);
                break;
                //运营
            case R.id.citypartnerpeole_system:
                Intent intent2 = new Intent(getActivity(), WebActivity.class)
                        .putExtra("url", OkeHomeConsumptionApi.ELVDOU_BASEURL_H5_LIFE + "app/h5/contract/7.html");
                startActivity(intent2);
                break;
                //商家邀请
            case R.id.citypartnerpeole_merchants_invate:
                Intent intent3 = new Intent(getActivity(), WebActivity.class)
                        .putExtra("url",OkeHomeConsumptionApi.ELVDOU_BASEURL_H5_LIFE + "app/h5/platform/14.html");
                startActivity(intent3);
                break;
                //装企邀请
            case R.id.citypartnerpeole_company_invate:
                Intent intent4 = new Intent(getActivity(), WebActivity.class)
                        .putExtra("url",OkeHomeConsumptionApi.ELVDOU_BASEURL_H5_LIFE + "app/h5/platform/15.html");
                startActivity(intent4);
                break;
            case R.id.citypartnerpeole_company_recruit:
                //装企招募
                startActivity(EnterpriseRecruitActivity.class);
                break;
            case R.id.citypartnerpeole_despoit:
                //可提现金额
                    Intent intent5 = new Intent(getActivity(), CityWalletActivity.class);
                    startActivity(intent5);

                break;
            case R.id.zongshouru_lin:
                //总收入记录
                Intent intent1 = new Intent(getActivity(), RevenueActivity.class);
                startActivity(intent1);
                break;
            case R.id.yitixian_lin:
                //已提现
                Intent intent7 = new Intent(getActivity(), LookpayeeActivity.class);
                intent7.putExtra("flage",2);
                intent7.putExtra("extractCashRecordarr_list", (Serializable) extractCashRecordarr_list2);
                intent7.putExtra("extractCash",extractCash);
                startActivity(intent7);
                break;
            case R.id.yue_lin:
                //已提现
                Intent intent8 = new Intent(getActivity(), LookpayeeActivity.class);
                intent8.putExtra("flage",3);
                intent8.putExtra("extractCashRecordarr_list", (Serializable) extractCashRecordarr_list3);
                intent8.putExtra("extractCash",LaveMoney);
                startActivity(intent8);
                break;



        }
    }



    @Override
    public void WalletGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsonobj = jsonOBJ.optJSONObject("data");
                // 数据对接正常
                remake = JsonUtils.object(jsonobj.toString(), CitypantnerRemake.class);
                extractCash = remake.getExtractCash();
                LaveMoney = remake.getLaveMoney();
                todayIncome = remake.getTodayIncome();
                //今日收入
                citypartnerpeole_payee.setText(remake.getTodayIncome()+"");
                //总收入
                citypartnerpeole_icome.setText(remake.getTotalIncome());
                //已提现
                citypartnerpeole_alreadly_despoit.setText(remake.getExtractCash() + "");
                //预计收入
                citypartnerpeole_balance.setText(remake.getLaveMoney()+ "");
                //可提现
                citypartnerpeole_despoit.setText(remake.getAllowCash()+"");

                JSONArray todayPaymentRecord = jsonobj.optJSONArray("todayPaymentRecord");
                if(todayPaymentRecord.length()!=0){
                    //提现记录
                    extractCashRecordarr_list1 = new Gson().fromJson(todayPaymentRecord.toString(), new
                            TypeToken<ArrayList<CityLookpayeeBean>>() {
                            }.getType());
                }

                JSONArray extractCashRecordarr = jsonobj.optJSONArray("extractCashRecord");
                if(extractCashRecordarr.length()!=0){
                    //提现记录
                    extractCashRecordarr_list2 = new Gson().fromJson(extractCashRecordarr.toString(), new
                            TypeToken<ArrayList<CityLookpayeeBean>>() {
                            }.getType());
                }

                JSONArray laveMoneyRecord = jsonobj.optJSONArray("laveMoneyRecord");
                if(laveMoneyRecord.length()!=0){
                    //余额记录
                    extractCashRecordarr_list3 = new Gson().fromJson(laveMoneyRecord.toString(), new
                            TypeToken<ArrayList<CityLookpayeeBean>>() {
                            }.getType());
                }
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("ShopListGet", "钱包");
        }
    }
}
